package com.entrogy.service.impl;

import com.entrogy.bean.*;
import com.entrogy.dao.AppraisalMapper;
import com.entrogy.dao.CheckTimeMapper;
import com.entrogy.service.CheckTimeService;
import com.entrogy.utils.DateUtils;
import com.entrogy.utils.StrUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author 许红松
 * @date 2019/6/5 0005 下午 3:54
 */
@Service
public class CheckTimeServiceImpl implements CheckTimeService {

    @Autowired
    private CheckTimeMapper checkTimeMapper;// 考勤dao接口

    @Autowired
    private AppraisalMapper appraisalMapper;

    /**
     * 获取成绩列表
     *
     * @param map
     * @return
     */
    @Override
    public List<Map<String, Object>> getScoreList(Map<String, Object> map) {
        return checkTimeMapper.getScoreList(map);
    }

    ;

    //   @Autowired
    //   private

    /**
     * 按日获取考勤记录
     *
     * @param map
     * @return
     */
    @Override
    public List<Map<String, Object>> selectDay(Map<String, Object> map) {
        //按日获取考勤记录
        List<Map<String, Object>> list1 = new ArrayList<>();
        List<Map<String, Object>> list3 = checkTimeMapper.getPeopleList(map);
        String totay = DateUtils.GetNowDate2().substring(0, 10);
        List<Map<String, Object>> list = checkTimeMapper.selectDay(map);
        if (list3 != null) {
            for (Map<String, Object> map4 : list3) {
                for (Map<String, Object> map1 : list) {
                    if (map4.get("user_Id").toString().equals(map1.get("person_id").toString())) {
                        //如果考勤设置中的用户id 和流水id中用户id 相同
                        Map<String, Object> map3 = getTimeBypeople(map4.get("user_Id").toString(), map.get("Date1").toString());//获取某人的考勤数据
                        if (map1.get("machine_name").toString().contains("YDM")) {//进入校门的时候
                            //  小于9点时候 视为上午上班
                            if (!DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Uptime").toString())) {
                                map4.put("morningWorkUp", map1.get("record_time").toString().substring(0, 19));     //早上上班时间
                                map4.put("morningWorkUpType", 1);//早上上班
                                map4.put("morningWorktimeUpResult", "Normal");
                            }//早上上班 正常
                            else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Uptime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Downtime").toString())) {//9点-12点进入
                                map4.put("morningWorkUp", map1.get("record_time").toString().substring(0, 19));     //早上上班时间
                                map4.put("morningWorkUpType", 1);//早上上班
                                map4.put("morningWorktimeUpResult", "Late");//早上上班迟到
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Downtime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Uptime").toString())) {//12-2点
                                map4.put("afternoonWorkUp", map1.get("record_time").toString().substring(0, 19));     //中午上班时间
                                map4.put("afternoonWorkUpType", 3);//中午上班
                                map4.put("afternoonWorktimeUpResult", "Normal");//中午上班正常
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Uptime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Downtime").toString())) {//2-6点
                                map4.put("afternoonWorkUp", map1.get("record_time").toString().substring(0, 19));     //中午上班时间
                                map4.put("afternoonWorkUpType", 3);//中午上班
                                map4.put("afternoonWorktimeUpResult", "Late");//中午上班迟到
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Downtime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Uptime").toString())) {//6-7.30点
                                map4.put("eveningWorkUp", map1.get("record_time").toString().substring(0, 19));     //晚上上班时间
                                map4.put("eveningWorkUpType", 5);//晚上上班
                                map4.put("eveningWorktimeUpResult", "Normal");//晚上上班正常
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Uptime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Downtime").toString())) {//7.30-9点
                                map4.put("eveningWorkUp", map1.get("record_time").toString().substring(0, 19));     //晚上上班时间
                                map4.put("eveningWorkUpType", 5);//晚上上班
                                map4.put("eveningWorktimeUpResult", "Late");//晚上上班迟到
                            }
                        } else if (map1.get("machine_name").toString().contains("ZDM")) {//出校门的时候
                            if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Uptime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Downtime").toString())) {//9点-12点
                                map4.put("morningWorkDown", map1.get("record_time").toString().substring(0, 19));     //早上下班时间
                                map4.put("morningWorkDownType", 2);//早上下班
                                map4.put("morningWorktimeDownResult", "Early");//早上下班早退
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Downtime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Uptime").toString())) {//12-2点
                                map4.put("morningWorkDown", map1.get("record_time").toString().substring(0, 19));     //早上下班时间
                                map4.put("morningWorkDownType", 2);//早上下班
                                map4.put("morningWorktimeDownResult", "Normal");//早上下班正常
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Uptime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Downtime").toString())) {//2-6点
                                map4.put("afternoonWorkDown", map1.get("record_time").toString().substring(0, 19));     //下午下班时间
                                map4.put("afternoonWorkDownType", 4);//下午下班
                                map4.put("afternoonWorktimeDownResult", "Early");//下午下班早退
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Downtime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Uptime").toString())) {//6-7.30点
                                map4.put("afternoonWorkDown", map1.get("record_time").toString().substring(0, 19));     //下午下班时间
                                map4.put("afternoonWorkDownType", 4);//下午下班
                                map4.put("afternoonWorktimeDownResult", "Normal");//下午下班正常
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Uptime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Downtime").toString())) {//7.30-9点
                                map4.put("eveningWorkDown", map1.get("record_time").toString().substring(0, 19));     //晚上下班时间
                                map4.put("eveningWorkDownType", 6);//晚上下班
                                map4.put("eveningWorktimeDownResult", "Early");//晚上下班早退
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Downtime").toString())) {//9点以后
                                map4.put("eveningWorkDown", map1.get("record_time").toString().substring(0, 19));     //晚上下班时间
                                map4.put("eveningWorkDownType", 6);//晚上下班
                                map4.put("eveningWorktimeDownResult", "Normal");//晚上下班正常
                            }
                        }

                    }

                }
                list1.add(map4);
            }

        }


        return list1;
    }

    /**
     * 根据此人的ID 查询 12点之后的最新下午进门时间
     *
     * @param map
     * @return
     */
    @Override
    public PassRecordBean getAfternoonTime(Map<String, Object> map) {
        Integer count = checkTimeMapper.getAfternoonTime1(setTableName(map));
        if (count != null) {
            if (count < 2) { // 中午的打卡 没有 两次以上 所以记录 他 下午上班 没有打卡
                return null;
            }
            return checkTimeMapper.getAfternoonTime2(setTableName(map));
        }
        return null;
    }

    /**
     * @param map
     * @return
     */
    @Override
    public PassRecordBean getMorningOutTime(Map<String, Object> map) {
        return checkTimeMapper.getMorningOutTime(setTableName(map));
    }

    /**
     * @param map
     * @return
     */
    @Override
    public PassRecordBean getAfternoonOutTime(Map<String, Object> map) {
        return checkTimeMapper.getAfternoonOutTime(setTableName(map));
    }

    /**
     * 根据月份去判断有没有这个月的统计数据
     *
     * @param map
     * @return
     */
    @Override
    public int checkMonthTable(Map<String, Object> map) {
        return checkTimeMapper.checkMonthTable(setTableName(map));
    }

    /**
     * 将表名存入map中
     *
     * @param map
     * @return
     */
    @Override
    public Map<String, Object> setTableName(Map<String, Object> map) {
        String tableName = "";
        if (map.containsKey("month")) {
            tableName = "pass_record_".concat(map.get("month").toString().replace("-", ""));
        }

        if (StringUtils.isNoneBlank(tableName)) {
            map.put("tableName", tableName);
        }
        return map;
    }

    ;

    /**
     * 创建这个月的总统计表
     *
     * @param map
     * @return
     */
    @Override
    public void createMonthTable(Map<String, Object> map) {
        //1 新建表
        checkTimeMapper.createMonthTable(setTableName(map));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sFormat = new SimpleDateFormat("HH:mm");
        List<String> days = getMonthDays(map.get("month").toString());
        // 按每月的天数 去每天生成的表里查询数据
        for (String day : days) {
            String tableName = "pass_record".concat(day);
            // 所有的考勤 人员 信息
            String sql2 = "select * from (select * from " + tableName
                    + " p,teacher_information t where t.isAttendance = 1 and p.person_id = t.teacher_thdid and machine_name like '%DT%') pp ,duty_information d where pp.admin_duty_id = d.duty_id";
            // 查询出每天的考勤打卡数据
            List<PassRecordBean> passRecordBeans = checkTimeMapper.getPassRecordList(map);
            String year = day.substring(0, 4).concat("-");
            String monthTime = day.substring(4, 6).concat("-");
            String dayTime = day.substring(6, 8);
            day = year + monthTime + dayTime;
            // 插入每天的打卡数据到每月的统计数据表中
            for (PassRecordBean passRecordBean : passRecordBeans) {
                String morningTime = null;// 上午上班打卡时间
                String morningOutTime = null;// 上午下班打卡时间
                String afternoonTime = null;// 下午上班打卡时间
                String afternoonOutTime = null;// 下午下班打卡时间
                String nightWork = null;// 晚上上班打卡时间
                String nightOutWork = null;// 晚上下班打卡时间
                Map<String, Object> map2 = new HashMap<>();
                map.put("user_Id", passRecordBean.getPerson_id());
                Map<String, Object> map1 = appraisalMapper.getshiftBypeople(map2);
                // 查询出某个人 的打卡时间
                String now = DateUtils.GetNowDate().substring(0, 10);
                if (null != map1) {

                    if (!StringUtils.isNotBlank(map1.get("shift1_Uptime").toString())) {
                        StringBuffer buf = new StringBuffer();
                        morningTime = buf.append(now).append(" ").append(map1.get("shift1_Uptime").toString()).append(":00").toString();
                    }
                    if (!StringUtils.isNotBlank(map1.get("shift1_Downtime").toString())) {
                        StringBuffer buf = new StringBuffer();
                        morningOutTime = buf.append(now).append(" ").append(map1.get("shift1_Downtime")).append(":00").toString();
                    }
                    if (!StringUtils.isNotBlank(map1.get("shift2_Uptime").toString())) {
                        StringBuffer buf = new StringBuffer();
                        afternoonTime = buf.append(now).append(" ").append(map1.get("shift2_Uptime")).append(":00").toString();
                    }
                    if (!StringUtils.isNotBlank(map1.get("shift2_Downtime").toString())) {
                        StringBuffer buf = new StringBuffer();
                        afternoonOutTime = buf.append(now).append(" ").append(map1.get("shift2_Downtime")).append(":00").toString();
                    }
                    if (map1.get("shift3_Uptime") != null && !"".equals(map1.get("shift3_Uptime"))) {
                        StringBuffer buf = new StringBuffer();
                        nightWork = buf.append(now).append(" ").append(map1.get("shift3_Uptime")).append(":00").toString();
                    }
                    if (map1.get("shift3_Downtime") != null && !"".equals(map1.get("shift3_Downtime"))) {
                        StringBuffer buf = new StringBuffer();
                        nightOutWork = buf.append(now).append(" ").append(map1.get("shift3_Downtime")).append(":00").toString();
                    }
                }

                // 判断是否打卡正常
                if (morningTime != null && !morningTime.equals(passRecordBean.getRecord_time()) && !DateUtils.compare(morningTime, passRecordBean.getRecord_time().toString())) {// 有早班在上班时间之后打的卡
                    passRecordBean.setIsNormal(1); // 0--正常 1--不正常
                } else if (afternoonTime != null && morningTime == null && !morningTime.equals(passRecordBean.getRecord_time())
                        && !DateUtils.compare(afternoonTime, passRecordBean.getRecord_time().toString())) {// 没有早班 在上班时间之后打的卡
                    passRecordBean.setIsNormal(1); // 0--正常 1--不正常
                } else if (morningTime != null && afternoonTime != null && passRecordBean.getIsNormal() != 1) {// 有早班和午班查看中午是否有两次打卡
                    Map<String, Object> map3 = new HashMap<>();
                    map3.put("person_id", passRecordBean.getPerson_id());
                    map3.put("morningOutTime", morningOutTime);
                    map3.put("afternoonTime", afternoonTime);
                    int counts = checkTimeMapper.checkMonthTable(setTableName(map3));
                    if (counts < 2) {
                        passRecordBean.setIsNormal(1); // 不正常
                    }

                } else if (passRecordBean.getIsNormal() != 1) { // 去判断下班 打卡是否正常
                    PassRecordBean passRecordBean1 = null;
                    if (StrUtils.isNullOrEmpty(map1.get("shift3_Uptime").toString()) && afternoonTime != null) {// 没有加班时间
                        // 下午下班时间后 的第一次打卡时间
                        Map<String, Object> map3 = new HashMap<>();
                        map3.put("person_id", passRecordBean.getPerson_id());
                        map3.put("nightOutWork", afternoonTime);
                        passRecordBean1 = getNightOutWork(setTableName(map3));
                    } else if (afternoonOutTime != null) {
                        // 下班时间后 的最后一次打卡时间
                        Map<String, Object> map3 = new HashMap<>();
                        map3.put("person_id", passRecordBean.getPerson_id());
                        map3.put("afternoonOutWork", afternoonOutTime);
                        passRecordBean1 = getAfternoonOutTime(setTableName(map3));
                    }
                    if (passRecordBean1 == null) {
                        passRecordBean.setIsNormal(1); // 不正常
                    }
                }
                // 判断这个人是否有加班时间
                if (!StrUtils.isNullOrEmpty(nightWork)) {// 有加班
                    if (passRecordBean.getIsNormal() != 1) {
                        Map<String, Object> map3 = new HashMap<>();
                        map3.put("person_id", passRecordBean.getPerson_id());
                        map3.put("afternoonOutTime", afternoonOutTime);
                        map3.put("nightWork", nightWork);
                        int counts = checkTimeMapper.checkMonthTable(setTableName(map3));
                        if (counts < 2) {
                            passRecordBean.setIsNormal(1); // 不正常
                        }
                    } else if (passRecordBean.getIsNormal() != 1) {
                        Map<String, Object> map3 = new HashMap<>();
                        map3.put("person_id", passRecordBean.getPerson_id());
                        map3.put("nightOutWork", nightOutWork);
                        PassRecordBean passRecordBean2 = getNightOutWork(setTableName(map3));// 下班时间后
                        // 的最后一次打卡时间
                        if (passRecordBean2 == null) {
                            passRecordBean.setIsNormal(1); // 不正常
                        }
                    }
                }
                Map<String, Object> map4 = new HashMap<>();
                map4.put("person_id", passRecordBean.getPerson_id());
                map4.put("person_name", passRecordBean.getPerson_name());
                map4.put("machine_name", passRecordBean.getMachine_name());
                map4.put("record_time", passRecordBean.getRecord_time());
                map4.put("isNormal", passRecordBean.getIsNormal());
                checkTimeMapper.insertpass(setTableName(map4));

            }
        }
    }


    /**
     * @param map
     * @return
     */
    @Override
    public List<String> getMonthDays(String date, Map<String, Object> map) {
        return null;
    }

    /**
     * 根据月份查询 考勤打卡的数据
     *
     * @param map
     * @return
     */
    @Override
    public List<UserEntity> getPassRecordByMoth(Map<String, Object> map) {
        UserEntity userEntity1 = (UserEntity) SecurityUtils.getSubject().getSession().getAttribute("currentUser");
        if (userEntity1 != null) {
            map.put("school_code", userEntity1.getSchoolCode());
        }
        // 1.先查询出 考勤打卡的所有人员信息
        //  String sql = "select teacher_thdid,d.duty_name as teacher_nickname,teacher_name from teacher_information t,duty_information d where t.isAttendance = 1 and t.admin_duty_id = d.duty_id";
        List<UserEntity> lists = checkTimeMapper.getPassRecordByMoth(setTableName(map));
        // 2.根据人员ID 按月去统计一个人的异常信息
        for (UserEntity userEntity : lists) {
            if (userEntity.getId() != null) {
                //查询的异常打卡次数
                map.put("isNormal", 1);
                map.put("personId", userEntity.getId());
                int count = checkTimeMapper.getcountsByUser(map);
                userEntity.setAbnormalClock(count);// 异常打卡次数
                // 3.异常打卡次数 总的打卡次数 减去正常的打卡次数
                if (map.containsKey("isNormal")) {
                    map.remove("isNormal");
                }
                int totalCount = checkTimeMapper.getcountsByUser(map);
                userEntity.setNormalClock(totalCount - count);
            } else {
                userEntity.setAbnormalClock(0);// 异常打卡次数
                userEntity.setNormalClock(0);
            }
        }
        return lists;
    }

    /**
     * @param map
     * @return
     */
    @Override
    public List<PassRecordBean> getInfoByThdid(Map<String, Object> map) {
        return null;
    }

    /**
     * @param map
     * @return
     */
    @Override
    public PassRecordBean getNightOutWork(Map<String, Object> map) {
        return checkTimeMapper.getNightOutWork(setTableName(map));
    }

    @Override
    public void getKaoqinTime(String date, String tableName, List<PassRecordBean> lists, int type) {

    }

    /**
     * 获取月份的天数
     *
     * @param date
     * @return
     */
    @Override
    public List<String> getMonthDays(String date) {
        List<String> resultList = new ArrayList<String>();
        if (StrUtils.isNullOrEmpty(date)) {
            return resultList;
        }
        int year = Integer.valueOf(date.substring(0, 4));
        int month = Integer.valueOf(date.substring(4, 6));
        // 在使用set方法之前，必须先clear一下，否则很多信息会继承自系统当前时间
        Calendar time = Calendar.getInstance();
        time.clear();
        time.set(Calendar.YEAR, year);
        time.set(Calendar.MONTH, month - 1);// Calendar对象默认一月为0
        int days = time.getActualMaximum(Calendar.DAY_OF_MONTH);// 本月份的天数
        for (int i = 1; i <= days; i++) {
            String day = "";
            if (i < 10) {
                day = date.substring(0, 6) + "0" + i;
            } else {
                day = date.substring(0, 6) + i;
            }
            resultList.add(day);
        }
        return resultList;
    }

    @Override
    public List<Map<String, Object>> getPeopleList(Map<String, Object> map) {
        return checkTimeMapper.getPeopleList(map);
    }


    /**
     * 获取某人的考勤时间
     *
     * @param user_Id
     * @return
     */
    private Map<String, Object> getTimeBypeople(String user_Id, String Date1) {
        String now = DateUtils.GetNowDate().substring(0, 10);
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map2 = new HashMap<>();
        map.put("user_Id", user_Id);
        map.put("tableName", "pass_record".concat(now.replace("_", "")));
        String week = DateUtils.getWeekOfDate(Date1);
        if (week.equals("星期一")) {
            map.put("shift", "shift1Id");
        } else if (week.equals("星期二")) {
            map.put("shift", "shift2Id");
        } else if (week.equals("星期三")) {
            map.put("shift", "shift3Id");
        } else if (week.equals("星期四")) {
            map.put("shift", "shift4Id");
        } else if (week.equals("星期五")) {
            map.put("shift", "shift5Id");
        } else if (week.equals("星期六")) {
            map.put("shift", "shift6Id");
        } else if (week.equals("星期日")) {
            map.put("shift", "shift7Id");
        }
        Map<String, Object> map1 = appraisalMapper.getshiftBypeople(map);
        String shift1_Uptime = "";
        String shift1_Downtime = "";
        String shift2_Uptime = "";
        String shift2_Downtime = "";
        String shift3_Uptime = "";
        String shift3_Downtime = "";
        if (map1 != null && !"".equals(map1)) {
            if (map1.get("shift1_Uptime") != null && !"".equals(map1.get("shift1_Uptime"))) {
                shift1_Uptime = Date1 + " " + map1.get("shift1_Uptime") + ":00";
                map2.put("shift1_Uptime", shift1_Uptime);
            }
            if (map1.get("shift1_Downtime") != null && !"".equals(map1.get("shift1_Downtime"))) {
                shift1_Downtime = Date1 + " " + map1.get("shift1_Downtime") + ":00";
                map2.put("shift1_Downtime", shift1_Downtime);
            }
            if (map1.get("shift2_Uptime") != null && !"".equals(map1.get("shift2_Uptime"))) {
                shift2_Uptime = Date1 + " " + map1.get("shift2_Uptime") + ":00";
                map2.put("shift2_Uptime", shift2_Uptime);
            }
            if (map1.get("shift2_Downtime") != null && !"".equals(map1.get("shift2_Downtime"))) {
                shift2_Downtime = Date1 + " " + map1.get("shift2_Downtime") + ":00";
                map2.put("shift2_Downtime", shift2_Downtime);
            }
            if (map1.get("shift3_Uptime") != null && !"".equals(map1.get("shift3_Uptime"))) {
                shift3_Uptime = Date1 + " " + map1.get("shift3_Uptime") + ":00";
                map2.put("shift3_Uptime", shift3_Uptime);
            }
            if (map1.get("shift3_Downtime") != null && !"".equals(map1.get("shift3_Downtime"))) {
                shift3_Downtime = Date1 + " " + map1.get("shift3_Downtime") + ":00";
                map2.put("shift3_Downtime", shift3_Downtime);
            }

        }
        return map2;
    }

    /**
     * 按日获取考勤记录  存取到考勤的总表中
     *
     * @param map
     * @return
     */
    @Override
    public List<Attendance> selectDay1(Map<String, Object> map) {
        //按日获取考勤记录
        List<Attendance> list1 = new ArrayList<>();
        List<Map<String, Object>> list3 = checkTimeMapper.getPeopleList(map);
        String totay = DateUtils.GetNowDate2().substring(0, 10);
        List<Map<String, Object>> list = checkTimeMapper.selectDay(map);
        if (list3 != null) {
            //map4.get("school_code")
            for (Map<String, Object> map4 : list3) {
                Attendance attendance = new Attendance();
                for (Map<String, Object> map1 : list) {
                    attendance.setAttendance_id("attendance".concat(String.valueOf(System.currentTimeMillis())));
                    attendance.setId(String.valueOf(System.currentTimeMillis()));
                    attendance.setSchool_code(Integer.parseInt(map4.get("school_code").toString()));
                    attendance.setGroupId(map4.get("attendance_Id").toString());
                    attendance.setWorkDate(map.get("Date1").toString());
                    attendance.setRecordId("暂无");
                    attendance.setUserId(map4.get("user_Id").toString());
                    attendance.setSourceType("FACE_CHECK");
                    attendance.setLocationResult("学校");
                    attendance.setUserCheckTime(map1.get("record_time").toString().substring(0, 19));
                    if (map4.get("user_Id").toString().equals(map1.get("person_id").toString())) {
                        //如果考勤设置中的用户id 和流水id中用户id 相同
                        Map<String, Object> map3 = getTimeBypeople(map4.get("user_Id").toString(), map.get("Date1").toString());//获取某人的考勤数据
                        if (map1.get("machine_name").toString().contains("YDM")) {//进入校门的时候
                            //  小于9点时候 视为上午上班
                            if (!DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Uptime").toString())) {
                                attendance.setBaseCheckTime(map3.get("shift1_Uptime").toString());
                                attendance.setCheckType("OnDuty");
                                attendance.setTimeResult("Normal");
                                attendance.setStype(1);
                                attendance.setPlanId(map4.get("shift1Id").toString());
                            }//早上上班 正常
                            else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Uptime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Downtime").toString())) {//9点-12点进入
                                attendance.setBaseCheckTime(map3.get("shift1_Uptime").toString());
                                attendance.setCheckType("OnDuty");
                                attendance.setTimeResult("Late");
                                attendance.setStype(1);
                                attendance.setPlanId(map4.get("shift1Id").toString());
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Downtime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Uptime").toString())) {//12-2点
                                attendance.setBaseCheckTime(map3.get("shift2_Uptime").toString());
                                attendance.setCheckType("OnDuty");
                                attendance.setTimeResult("Normal");
                                attendance.setStype(3);
                                attendance.setPlanId(map4.get("shift3Id").toString());
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Uptime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Downtime").toString())) {//2-6点
                                attendance.setBaseCheckTime(map3.get("shift2_Uptime").toString());
                                attendance.setCheckType("OnDuty");
                                attendance.setTimeResult("Late");
                                attendance.setStype(3);
                                attendance.setPlanId(map4.get("shift3Id").toString());
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Downtime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Uptime").toString())) {//6-7.30点
                                attendance.setBaseCheckTime(map3.get("shift3_Uptime").toString());
                                attendance.setCheckType("OnDuty");
                                attendance.setTimeResult("Normal");
                                attendance.setStype(5);
                                attendance.setPlanId(map4.get("shift5Id").toString());
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Uptime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Downtime").toString())) {//7.30-9点
                                attendance.setBaseCheckTime(map3.get("shift2_Uptime").toString());
                                attendance.setCheckType("OnDuty");
                                attendance.setTimeResult("Late");
                                attendance.setStype(5);
                                attendance.setPlanId(map4.get("shift5Id").toString());
                            }
                        } else if (map1.get("machine_name").toString().contains("ZDM")) {//出校门的时候
                            if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Uptime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Downtime").toString())) {//9点-12点
                                attendance.setBaseCheckTime(map3.get("shift1_Downtime").toString());
                                attendance.setCheckType("OffDuty");
                                attendance.setTimeResult("Early");
                                attendance.setStype(2);
                                attendance.setPlanId(map4.get("shift2Id").toString());
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift1_Downtime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Uptime").toString())) {//12-2点
                                attendance.setBaseCheckTime(map3.get("shift1_Downtime").toString());
                                attendance.setCheckType("OffDuty");
                                attendance.setTimeResult("Normal");
                                attendance.setStype(2);
                                attendance.setPlanId(map4.get("shift2Id").toString());
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Uptime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Downtime").toString())) {//2-6点
                                attendance.setBaseCheckTime(map3.get("shift2_Downtime").toString());
                                attendance.setCheckType("OffDuty");
                                attendance.setTimeResult("Early");
                                attendance.setStype(4);
                                attendance.setPlanId(map4.get("shift14d").toString());
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift2_Downtime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Uptime").toString())) {//6-7.30点
                                attendance.setBaseCheckTime(map3.get("shift2_Downtime").toString());
                                attendance.setCheckType("OffDuty");
                                attendance.setTimeResult("Normal");
                                attendance.setStype(4);
                                attendance.setPlanId(map4.get("shift4Id").toString());
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Uptime").toString()) && !DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Downtime").toString())) {//7.30-9点
                                attendance.setBaseCheckTime(map3.get("shift3_Downtime").toString());
                                attendance.setCheckType("OffDuty");
                                attendance.setTimeResult("Early");
                                attendance.setStype(6);
                                attendance.setPlanId(map4.get("shift6Id").toString());
                            } else if (DateUtils.compare(map1.get("record_time").toString(), map3.get("shift3_Downtime").toString())) {//9点以后
                                attendance.setBaseCheckTime(map3.get("shift3_Downtime").toString());
                                attendance.setCheckType("OffDuty");
                                attendance.setTimeResult("Normal");
                                attendance.setStype(6);
                                attendance.setPlanId(map4.get("shift6Id").toString());
                            }
                        }

                    }

                }
                list1.add(attendance);
            }

        }


        return list1;
    }


    /**
     * 按日获取考勤记录  存取到考勤的总表中
     *
     * @param map
     * @return
     */
    @Override
    public List<Attendance> selectDay2(Map<String, Object> map) {
        //按日获取考勤记录


        List<Attendance> list1 = new ArrayList<>();
        List<Map<String, Object>> list3 = checkTimeMapper.getPeopleList(map);
        /* String totay=DateUtils.GetNowDate2().substring(0, 11);*/

        if (list3 != null) {
            //map4.get("school_code")
            for (Map<String, Object> map4 : list3) {

                String table_name = "pass_record" + map.get("Date1").toString().substring(0, 10).replace("-", "");
                map.put("userId", map4.get("user_Id"));
                map.put("tableName", table_name);
                List<Map<String, Object>> list = checkTimeMapper.selectDay(map);
                Map<String, Object> map3 = getTimeBypeople(map4.get("user_Id").toString(), map.get("Date1").toString());//获取某人的考勤数据

                if (list != null) {
                    map.put("downTime", map3.get("shift1_Uptime").toString());
                    List<Map<String, Object>> list4 = checkTimeMapper.selectDay(map);
                    if (list4.size() > 0) {
                        Attendance attendance = new Attendance();
                        attendance.setUserName(map4.get("name").toString());
                        attendance.setAttendance_id("attendance".concat(String.valueOf(System.currentTimeMillis())));
                        attendance.setId(String.valueOf(System.currentTimeMillis()));
                        attendance.setSchool_code(Integer.parseInt(map4.get("school_code").toString()));
                        attendance.setGroupId(map4.get("attendance_Id").toString());
                        attendance.setWorkDate(map.get("Date1").toString());
                        attendance.setRecordId("暂无");
                        attendance.setUserId(map4.get("user_Id").toString());
                        attendance.setSourceType("FACE_CHECK");
                        attendance.setLocationResult("学校");
                        attendance.setBaseCheckTime(map3.get("shift1_Uptime").toString());
                        attendance.setCheckType("OnDuty");
                        attendance.setTimeResult("Normal");
                        attendance.setStype(1);
                        attendance.setPlanId(map4.get("shift1Id").toString());
                        attendance.setIsok(1);
                        if (list4.size() > 0) {
                            attendance.setUserCheckTime(list4.get(0).get("record_time").toString().substring(0, 19));
                        }
                        list1.add(attendance);
                    } else {
                        Map<String, Object> map2 = new HashMap<>();
                        map2.put("userId", map4.get("user_Id"));
                        map2.put("startTime", map3.get("shift1_Uptime").toString());
                        /*  map2.put("DownTime",map3.get("shift1_Downtime").toString());*/
                        map2.put("tableName", table_name);
                        List<Map<String, Object>> list5 = checkTimeMapper.selectDay(map2);
                        if (list5 != null) {
                            Attendance attendance = new Attendance();
                            attendance.setUserName(map4.get("name").toString());
                            attendance.setAttendance_id("attendance".concat(String.valueOf(System.currentTimeMillis())));
                            attendance.setId(String.valueOf(System.currentTimeMillis()));
                            attendance.setSchool_code(Integer.parseInt(map4.get("school_code").toString()));
                            attendance.setGroupId(map4.get("attendance_Id").toString());
                            attendance.setWorkDate(map.get("Date1").toString());
                            attendance.setRecordId("暂无");
                            attendance.setUserId(map4.get("user_Id").toString());
                            attendance.setSourceType("FACE_CHECK");
                            attendance.setLocationResult("学校");
                            attendance.setBaseCheckTime(map3.get("shift1_Uptime").toString());
                            attendance.setCheckType("OnDuty");
                            attendance.setTimeResult("late");
                            attendance.setStype(1);
                            attendance.setPlanId(map4.get("shift1Id").toString());
                            attendance.setIsok(1);
                            if (list5.size() > 0) {
                                attendance.setUserCheckTime(list5.get(0).get("record_time").toString().substring(0, 19));
                            }
                            list1.add(attendance);
                        } else {
                            Attendance attendance = new Attendance();
                            attendance.setUserName(map4.get("name").toString());
                            attendance.setAttendance_id("attendance".concat(String.valueOf(System.currentTimeMillis())));
                            attendance.setId(String.valueOf(System.currentTimeMillis()));
                            attendance.setSchool_code(Integer.parseInt(map4.get("school_code").toString()));
                            attendance.setGroupId(map4.get("attendance_Id").toString());
                            attendance.setWorkDate(map.get("Date1").toString());
                            attendance.setRecordId("暂无");
                            attendance.setUserId(map4.get("user_Id").toString());
                            attendance.setSourceType("FACE_CHECK");
                            attendance.setLocationResult("学校");
                            attendance.setBaseCheckTime(map3.get("shift1_Uptime").toString());
                            attendance.setCheckType("OffDuty");
                            attendance.setTimeResult("NotSigned");
                            attendance.setStype(2);
                            attendance.setPlanId(map4.get("shift1Id").toString());
                            attendance.setIsok(1);

                            list1.add(attendance);
                        }
                    }
                    // 如果9-16有数据  16以后没数据
                    Map<String, Object> map2 = new HashMap<>();
                    map2.put("startTime", map3.get("shift1_Uptime").toString());
                    /*  map2.put("DownTime",map3.get("shift1_Downtime").toString());*/
                    map2.put("endTime", map3.get("shift1_Downtime").toString());
                    map2.put("userId", map4.get("user_Id"));
                    map2.put("tableName", table_name);
                    List<Map<String, Object>> list5 = checkTimeMapper.selectDay(map2);
                    Map<String, Object> map7 = new HashMap<>();
                    map7.put("startTime", map3.get("shift1_Downtime").toString());
                    map7.put("tableName", table_name);
                    map7.put("userId", map4.get("user_Id"));
                    List<Map<String, Object>> list6 = checkTimeMapper.selectDay(map7);
                    if (list6.size() > 0) {
                        if (list5 != null) {
                            Attendance attendance = new Attendance();
                            attendance.setUserName(map4.get("name").toString());
                            attendance.setAttendance_id("attendance".concat(String.valueOf(System.currentTimeMillis())));
                            attendance.setId(String.valueOf(System.currentTimeMillis()));
                            attendance.setSchool_code(Integer.parseInt(map4.get("school_code").toString()));
                            attendance.setGroupId(map4.get("attendance_Id").toString());
                            attendance.setWorkDate(map.get("Date1").toString());
                            attendance.setRecordId("暂无");
                            attendance.setUserId(map4.get("user_Id").toString());
                            attendance.setSourceType("FACE_CHECK");
                            attendance.setLocationResult("学校");
                            attendance.setBaseCheckTime(map3.get("shift1_Downtime").toString());
                            attendance.setCheckType("OffDuty");
                            attendance.setTimeResult("NotSigned");
                            attendance.setStype(2);
                            attendance.setPlanId(map4.get("shift1Id").toString());
                            attendance.setIsok(1);

                            list1.add(attendance);
                        } else {
                            Attendance attendance = new Attendance();
                            attendance.setUserName(map4.get("name").toString());
                            attendance.setAttendance_id("attendance".concat(String.valueOf(System.currentTimeMillis())));
                            attendance.setId(String.valueOf(System.currentTimeMillis()));
                            attendance.setSchool_code(Integer.parseInt(map4.get("school_code").toString()));
                            attendance.setGroupId(map4.get("attendance_Id").toString());
                            attendance.setWorkDate(map.get("Date1").toString());
                            attendance.setRecordId("暂无");
                            attendance.setUserId(map4.get("user_Id").toString());
                            attendance.setSourceType("FACE_CHECK");
                            attendance.setLocationResult("学校");
                            attendance.setBaseCheckTime(map3.get("shift1_Downtime").toString());
                            attendance.setCheckType("OffDuty");
                            attendance.setTimeResult("Normal");
                            attendance.setStype(2);
                            attendance.setPlanId(map4.get("shift1Id").toString());
                            attendance.setIsok(1);
                            attendance.setUserCheckTime(list6.get(list6.size() - 1).get("record_time").toString().substring(0, 19));
                            list1.add(attendance);
                        }
                    } else {
                        if (list5 != null && list6.size() == 0) {
                            Attendance attendance = new Attendance();
                            attendance.setUserName(map4.get("name").toString());
                            attendance.setAttendance_id("attendance".concat(String.valueOf(System.currentTimeMillis())));
                            attendance.setId(String.valueOf(System.currentTimeMillis()));
                            attendance.setSchool_code(Integer.parseInt(map4.get("school_code").toString()));
                            attendance.setGroupId(map4.get("attendance_Id").toString());
                            attendance.setWorkDate(map.get("Date1").toString());
                            attendance.setRecordId("暂无");
                            attendance.setUserId(map4.get("user_Id").toString());
                            attendance.setSourceType("FACE_CHECK");
                            attendance.setLocationResult("学校");
                            attendance.setBaseCheckTime(map3.get("shift1_Downtime").toString());
                            attendance.setCheckType("OffDuty");
                            attendance.setTimeResult("Early");
                            attendance.setStype(2);
                            attendance.setPlanId(map4.get("shift1Id").toString());
                            attendance.setIsok(1);
                            if (list5.size() > 0) {
                                attendance.setUserCheckTime(list5.get(list5.size() - 1).get("record_time").toString().substring(0, 19));
                            }
                            list1.add(attendance);
                        }
                    }

                }

            }
        }

        return list1;
    }


    /**
     * 根据条件查询考勤信息
     *
     * @param map
     * @return
     */
    @Override
    public List<Attendance> getAttendByType(Map<String, Object> map) {
        return checkTimeMapper.getAttendByType(map);
    }

    ;

    /**
     * 按日获取考勤记录  存取到考勤的总表中
     *
     * @param map
     * @return
     */
    @Override
    public List<CheckTime> selectDay3(Map<String, Object> map) {
        //按日获取考勤记录


        List<CheckTime> list1 = new ArrayList<>();
        List<Map<String, Object>> list3 = checkTimeMapper.getPeopleList(map);//考勤人
        if (list3 != null) {
            for (Map<String, Object> map4 : list3) {
                //表名
                String table_name = "pass_record" + map.get("Date1").toString().substring(0, 10).replace("-", "");
                map.put("userId", map4.get("user_thdid"));
                map.put("tableName", table_name);
                Map<String, Object> map3 = getTimeBypeople(map4.get("user_Id").toString(), map.get("Date1").toString());//获取某人的考勤数据
                if (map3.containsKey("shift1_Uptime")){
                Map<String, Object> map6 = new HashMap<>();
                map6.put("tableName", table_name);
                map6.put("userId", map4.get("user_thdid"));
                //查询某人当天的考勤记录
                List<Map<String, Object>> list20 = checkTimeMapper.selectByPersonId(map6);
                CheckTime checkTime = new CheckTime();
                checkTime.setUserName(map4.get("name").toString());
                /*  checkTime.setBaseCheckTime();*/
                checkTime.setUserId(map4.get("user_Id").toString());
                checkTime.setWorkDate(map.get("Date1").toString());
                //查询考勤组的某人在第一次上班之前的记录
                map.put("morningUpTime1", map3.get("shift1_Uptime").toString());
                List<Map<String, Object>> list4 = checkTimeMapper.selectDay(map);
                //查询考勤组的某人在第一次上班到第一次下班之前的记录
                if (map.containsKey("morningUpTime1")) {
                    map.remove("morningUpTime1");
                }
                map.put("morningUpTime", map3.get("shift1_Uptime").toString());
                map.put("morningDownTime", map3.get("shift1_Downtime").toString());
                List<Map<String, Object>> list5 = checkTimeMapper.selectDay(map);
                if (map.containsKey("morningUpTime")) {
                    map.remove("morningUpTime");
                }
                if (map.containsKey("morningDownTime")) {
                    map.remove("morningDownTime");
                }
                int list20Length = list20.size();
                int list4Length = list4.size();
                int list5Length = list5.size();
                if (map3.containsKey("shift3_Uptime")) {//代表一天三次上下班
                    //查询考勤组的某人在第一次下班到第二次上班的记录
                    map.put("morningDownTime1", map3.get("shift1_Downtime").toString());
                    map.put("afternoonUpTime", map3.get("shift2_Uptime").toString());
                    List<Map<String, Object>> list6 = checkTimeMapper.selectDay(map);
                    //查询考勤组的某人在第二次上班到第二次下班的记录
                    if (map.containsKey("morningDownTime1")) {
                        map.remove("morningDownTime1");
                    }
                    if (map.containsKey("afternoonUpTime")) {
                        map.remove("afternoonUpTime");
                    }
                    map.put("afternoonUpTime1", map3.get("shift2_Uptime").toString());
                    map.put("afternoonDownTime", map3.get("shift2_Downtime").toString());
                    List<Map<String, Object>> list7 = checkTimeMapper.selectDay(map);
                    //查询考勤组的某人在第二次下班到第三次上班记录
                    if (map.containsKey("afternoonUpTime1")) {
                        map.remove("afternoonUpTime1");
                    }
                    if (map.containsKey("afternoonDownTime")) {
                        map.remove("afternoonDownTime");
                    }
                    map.put("afternoonDownTime1", map3.get("shift2_Downtime").toString());
                    map.put("eveningUpTime", map3.get("shift3_Uptime").toString());
                    List<Map<String, Object>> list8 = checkTimeMapper.selectDay(map);
                    //查询考勤组的某人在第三次上班到第三次下班记录
                    if (map.containsKey("afternoonDownTime1")) {
                        map.remove("afternoonDownTime1");
                    }
                    if (map.containsKey("eveningUpTime")) {
                        map.remove("eveningUpTime");
                    }
                    map.put("eveningUpTime1", map3.get("shift3_Uptime").toString());
                    map.put("eveningDownTime", map3.get("shift3_Downtime").toString());
                    List<Map<String, Object>> list9 = checkTimeMapper.selectDay(map);
                    //查询考勤组的某人在第三次下班以后记录
                    if (map.containsKey("eveningUpTime1")) {
                        map.remove("eveningUpTime1");
                    }
                    if (map.containsKey("eveningDownTime")) {
                        map.remove("eveningDownTime");
                    }
                    map.put("eveningDownTime1", map3.get("shift3_Downtime").toString());
                    List<Map<String, Object>> list10 = checkTimeMapper.selectDay(map);
                    if (map.containsKey("eveningDownTime1")) {
                        map.remove("eveningDownTime1");
                    }
                    int list6Length = list6.size();
                    int list7Length = list7.size();
                    int list8Length = list8.size();
                    int list9Length = list9.size();
                    int list10Length = list10.size();
                    if (list20Length > 0) {//该人当天不存在 旷工
                        if (list4Length == 0 && list5Length != 0) {//第一次上班基点考勤前无数据  第一次下班前有数据
                            checkTime.setTimeResult1("Late");
                            checkTime.setMorningUpCheckTime(list5.get(0).get("record_time").toString());
                        }
                        if (list4Length != 0) {
                            checkTime.setTimeResult1("Normal");
                            checkTime.setMorningUpCheckTime(list4.get(0).get("record_time").toString());
                        }
                        if (list4Length == 0 && list5Length == 0) {
                            checkTime.setTimeResult1("NotSigned");
                        }
                        if (list6Length >= 2) {
                            checkTime.setTimeResult2("Normal");
                            checkTime.setTimeResult3("Normal");
                            checkTime.setMorningUpCheckTime(list6.get(0).get("record_time").toString());
                            checkTime.setAfternoonUpCheckTime(list6.get(list6.size() - 1).get("record_time").toString());
                        }
                        if (list6Length < 2 && list7Length != 0) {
                            checkTime.setTimeResult2("Normal");
                            checkTime.setTimeResult3("Late");
                            checkTime.setMorningUpCheckTime(list6.get(0).get("record_time").toString());
                            checkTime.setAfternoonUpCheckTime(list7.get(0).get("record_time").toString());
                        }
                        if (list8Length >= 2) {
                            checkTime.setTimeResult4("Normal");
                            checkTime.setTimeResult5("Normal");
                            checkTime.setAfternoonDownCheckTime(list8.get(0).get("record_time").toString());
                            checkTime.setEveningUpCheckTime(list8.get(list8.size() - 1).get("record_time").toString());
                        }
                        if (list8Length < 2 && list9Length != 0) {
                            checkTime.setTimeResult4("Normal");
                            checkTime.setTimeResult5("Late");
                            checkTime.setAfternoonDownCheckTime(list8.get(0).get("record_time").toString());
                            checkTime.setEveningUpCheckTime(list9.get(0).get("record_time").toString());
                        }
                        if (list10Length != 0) {
                            checkTime.setTimeResult6("Normal");
                            checkTime.setEveningDownCheckTime(list10.get(list10.size() - 1).get("record_time").toString());
                        } else {
                            checkTime.setTimeResult6("Early");
                            checkTime.setEveningDownCheckTime(list9.get(list9.size() - 1).get("record_time").toString());
                        }
                    } else {
                        checkTime.setTimeResult("NotSigned");

                    }

                }
                if (map3.containsKey("shift2_Uptime") && !map3.containsKey("shift3_Uptime")) {//代表一天两次上下班
                    //查询考勤组的某人在第一次下班到第二次上班的记录
                    map.put("morningDownTime1", map3.get("shift1_Downtime").toString());
                    map.put("afternoonUpTime", map3.get("shift2_Uptime").toString());
                    List<Map<String, Object>> list6 = checkTimeMapper.selectDay(map);
                    //查询考勤组的某人在第二次上班到第二次下班的记录
                    if (map.containsKey("morningDownTime1")) {
                        map.remove("morningDownTime1");
                    }
                    if (map.containsKey("afternoonUpTime")) {
                        map.remove("afternoonUpTime");
                    }
                    map.put("afternoonUpTime1", map3.get("shift2_Uptime").toString());
                    map.put("afternoonDownTime", map3.get("shift2_Downtime").toString());
                    List<Map<String, Object>> list7 = checkTimeMapper.selectDay(map);
                    //查询考勤组的某人在第二次下班后的记录
                    if (map.containsKey("afternoonUpTime1")) {
                        map.remove("afternoonUpTime1");
                    }
                    if (map.containsKey("afternoonDownTime")) {
                        map.remove("afternoonDownTime");
                    }
                    map.put("twoWorkDown", map3.get("shift2_Downtime").toString());
                    List<Map<String, Object>> list8 = checkTimeMapper.selectDay(map);
                    if (map.containsKey("twoWorkDown")) {
                        map.remove("twoWorkDown");
                    }
                    int list6Length = list6.size();
                    int list7Length = list7.size();
                    int list8Length = list8.size();

                    if (list20Length != 0) {//该人当天不存在 旷工
                        if (list4Length == 0 && list5Length != 0) {//第一次上班基点考勤前无数据  第一次下班前有数据
                            checkTime.setTimeResult1("Late");
                            checkTime.setMorningUpCheckTime(list5.get(0).get("record_time").toString());
                        }
                        if (list4Length != 0) {
                            checkTime.setTimeResult1("Normal");
                            checkTime.setMorningUpCheckTime(list4.get(0).get("record_time").toString());
                        }
                        if (list4Length == 0 && list5Length == 0) {
                            checkTime.setTimeResult1("NotSigned");
                        }
                        if (list6Length >= 2) {
                            checkTime.setTimeResult2("Normal");
                            checkTime.setTimeResult3("Normal");
                            checkTime.setMorningUpCheckTime(list6.get(0).get("record_time").toString());
                            checkTime.setAfternoonUpCheckTime(list6.get(list6.size() - 1).get("record_time").toString());
                        }
                        if (list6Length < 2 && list7Length != 0) {
                            checkTime.setTimeResult2("Normal");
                            checkTime.setTimeResult3("Late");
                            checkTime.setMorningUpCheckTime(list6.get(0).get("record_time").toString());
                            checkTime.setAfternoonUpCheckTime(list7.get(0).get("record_time").toString());
                        }
                        if (list8Length != 0) {
                            checkTime.setTimeResult4("Normal");
                            checkTime.setAfternoonDownCheckTime(list8.get(0).get("record_time").toString());
                        } else {
                            checkTime.setTimeResult4("Early");
                            checkTime.setAfternoonDownCheckTime(list7.get(list7.size() - 1).get("record_time").toString());
                        }
                    } else {
                        checkTime.setTimeResult("NotSigned");
                    }
                }
                if (!map3.containsKey("shift2_Uptime")) {//代表一天一次上下班
                    map.put("oneWorkDown", map3.get("shift1_Downtime").toString());
                    //查询考勤组的某人在第一次下班到之后记录
                    List<Map<String, Object>> list6 = checkTimeMapper.selectDay(map);
                    if (map.containsKey("oneWorkDown")) {
                        map.remove("oneWorkDown");
                    }
                    if (list20Length != 0) {//该人当天不存在 旷工
                        int list6Length = list6.size();

                        if (list4Length == 0 && list5Length > 0) {//第一次上班基点考勤前无数据  第一次下班前有数据
                            checkTime.setTimeResult1("Late");
                            checkTime.setMorningUpCheckTime(list5.get(0).get("record_time").toString());
                        }
                        if (list4Length > 0) {
                            checkTime.setTimeResult1("Normal");
                            checkTime.setMorningUpCheckTime(list4.get(0).get("record_time").toString());
                        }
                        if (list4Length == 0 && list5Length == 0) {
                            checkTime.setTimeResult1("NotSigned");
                        }
                        if (list6Length > 0) {
                            checkTime.setTimeResult2("Normal");
                            checkTime.setMorningDownCheckTime(list6.get(0).get("record_time").toString());
                        } else {
                            checkTime.setTimeResult2("Early");
                            if (list5Length > 0) {
                                checkTime.setMorningDownCheckTime(list5.get(list5.size() - 1).get("record_time").toString());
                            }
                        }
                    } else {
                        checkTime.setTimeResult("NotSigned");
                    }
                }
                list1.add(checkTime);
            }
            }

        }

        return list1;
    }

    /**
     *   统计
     * @param map
     * @return
     */
    @Override
    public  List<CheckTime>  getCountList( Map<String,Object> map){

       int a=Integer.parseInt(map.get("Time1").toString().replace("-","").substring(6, 8));
        int b=Integer.parseInt(map.get("Time2").toString().replace("-","").substring(6, 8));
       /* List<List<CheckTime>> list=new ArrayList<>();*/
       /* CheckTimeResult checkTimeResult=new CheckTimeResult();
          int   Earlynum=0;
          int   Normalnum=0;
          int   Latenum=0;
          int   NotSignednum=0;*/
        List<CheckTime> list2=new ArrayList<>();
        for (int i=a;i<b+1;i++){
            String Date1="";
            if (a<10) {
                String m="0"+i;
                 Date1 = map.get("Time1").toString().substring(0, 8).concat(m);
            }else{
                 Date1 = map.get("Time1").toString().substring(0, 8).concat(String.valueOf(i));
            }
           map.put("Date1",Date1);
           List<CheckTime>  list1=getcheckList(selectDay3(map));
         /*   list.add(list1);*/
            list2.addAll(list1);
        }
       /* checkTimeResult.setList(list);*/
        return list2;

    }

        private  List<CheckTime>   getcheckList(List<CheckTime> list){
            List<CheckTime>  list1=new ArrayList<>();
            for (CheckTime checkTime:list){
                int   Earlynum=0;
                int   Normalnum=0;
                int   Latenum=0;
                int   NotSignednum=0;
                if ("NotSigned".equals(checkTime.getTimeResult())){
                    NotSignednum++;
                }
                if ("Early".equals(checkTime.getTimeResult1())){
                    Earlynum++;
                }
                if ("Normal".equals(checkTime.getTimeResult1())){
                    Normalnum++;
                }
                if ("Late".equals(checkTime.getTimeResult1())){
                    Latenum++;
                }
                if ("Early".equals(checkTime.getTimeResult2())){
                    Earlynum++;
                }
                if ("Normal".equals(checkTime.getTimeResult2())){
                    Normalnum++;
                }
                if ("Late".equals(checkTime.getTimeResult2())){
                    Latenum++;
                }
                if ("Early".equals(checkTime.getTimeResult3())){
                    Earlynum++;
                }
                if ("Normal".equals(checkTime.getTimeResult3())){
                    Normalnum++;
                }
                if ("Late".equals(checkTime.getTimeResult3())){
                    Latenum++;
                }
                if ("Early".equals(checkTime.getTimeResult4())){
                    Earlynum++;
                }
                if ("Normal".equals(checkTime.getTimeResult4())){
                    Normalnum++;
                }
                if ("Late".equals(checkTime.getTimeResult4())){
                    Latenum++;
                }
                if ("Early".equals(checkTime.getTimeResult5())){
                    Earlynum++;
                }
                if ("Normal".equals(checkTime.getTimeResult5())){
                    Normalnum++;
                }
                if ("Late".equals(checkTime.getTimeResult5())){
                    Latenum++;
                }
                if ("Early".equals(checkTime.getTimeResult6())){
                    Earlynum++;
                }
                if ("Normal".equals(checkTime.getTimeResult6())){
                    Normalnum++;
                }
                if ("Late".equals(checkTime.getTimeResult6())){
                    Latenum++;
                }
                checkTime.setNotSignednum(NotSignednum);
                checkTime.setLatenum(Latenum);
                checkTime.setNormalnum(Normalnum);
                checkTime.setEarlynum(Earlynum);
                list1.add(checkTime);
            }
           return list1;
        }

         @Override
        public List<CheckTimeResult>   getMonthList(Map<String,Object> map){
            List<CheckTimeResult> list1=new ArrayList<>();
            //考勤人
            List<Map<String, Object>> list3 = checkTimeMapper.getPeopleList(map);//考勤人
             //考勤每日统计
             List<CheckTime>  list2=getCountList(map);
             if (list3!=null){
             for (Map<String, Object> map1:list3) {
                   int   WorkDaynum=0;
                   String   userName="";
                   int   Earlynum=0;
                   int   Normalnum=0;
                   int   Latenum=0;
                   int   NotSignednum=0;
                for (CheckTime checkTime:list2){
                    if (map1.get("user_Id").toString().equals(checkTime.getUserId())){
                        Earlynum+=checkTime.getEarlynum();
                        Normalnum+=checkTime.getNormalnum();
                        Latenum+=checkTime.getLatenum();
                        NotSignednum+=checkTime.getNotSignednum();
                        WorkDaynum++;
                    }
                }
                 CheckTimeResult result=new CheckTimeResult();
                result.setEarlynum(Earlynum);
                result.setLatenum(Latenum);
                result.setNormalnum(Normalnum);
                result.setNotSignednum(NotSignednum);
                result.setUserName(map1.get("name").toString());
                result.setWorkDaynum(WorkDaynum);
                 list1.add(result);
             }
             }
             return  list1;
        }
}





